<?php
/**
 * Author: Hancock
 * Email: 84622365@qq.com
 * Date: 8/8/2016
 * Des: 淘宝客
 */
include_once ROOT_PATH . "inc/ali/TopSdk.php";
date_default_timezone_set('Asia/Shanghai');

class tbk
{
    private $top_client;
    private $request;

    public function __construct($site_name=ALI_APP_KEY)
    {
        $this->top_client = new TopClient;
        $this->request = new TbkItemInfoGetRequest;
        $this->setAppKey($site_name);
        $this->setAppSecret($site_name);
    }

    /**
     * 设置key
     */
    public function setAppKey($site_name)
    {
        $mod = M("app.ali_app");
        $mod->setApp($site_name);
        $this->top_client->appkey = $mod->getKey();
    }

    /**
     * 设置secret
     */
    public function setAppSecret($site_name)
    {
        $mod = M("app.ali_app");
        $mod->setApp($site_name);
        $this->top_client->secretKey = $mod->getSecret();
    }

    public function getItemInfo($num_iid)
    {
        $num_iid = str_replace(',', "','", $num_iid);
        $this->request->setFields("num_iid,title,detail,detail_url,description,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,nick,seller_id");
        $this->request->setPlatform("1");
        $this->request->setNumIids($num_iid);
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }

    public function getItemDetail($num_iid){
        $num_iid = str_replace(',', "','", $num_iid);
        $this->request = new  TbkItemDetailGetRequest();
        $this->request->setFields("num_iid,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,description,item_click_url,shop_click_url");
        $this->request->setPlatform("1");
        $this->request->setNumIids($num_iid);
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }

    public function getFavoritesInfo($page)
    {   $page = str_replace(',', "','", $page);
        $this->request = new  TbkUatmFavoritesGetRequest;
        $this->request->setFields("favorites_title,favorites_id,type");
        $this->request->setPageNo($page);
        $this->request->setPageSize("20");
        $this->request->setType("-1");
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }

    public function getFavoritesClickUrl($ad_zone_id, $fav_id, $page, $page_size=100)
    {
        $page = str_replace(',', "','", $page);
        $this->request = new TbkUatmFavoritesItemGetRequest;
        $this->request->setFields("num_iid,click_url,title,pict_url,small_images,reserve_price,zk_final_price,user_type,provcity,item_url,nick,seller_id,tk_rate");
        $this->request->setPlatform("1");
        $this->request->setPageSize(str_replace(',', "','", $page_size));
        $this->request->setAdzoneId($ad_zone_id);
        $this->request->setFavoritesId($fav_id);
        $this->request->setPageNo($page);
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }
    public function getItemDesc($id){
        $item_url = M("Good")->getItemUrl($id);
        if($item_url===false){
            return false;
        }
        $desc = getTbItemDesc($item_url);
        return $desc;
    }
    public function getShareTpwd($logo,$title,$url){
        $this->request = new TbkTpwdCreateRequest;
        $this->request->setUserId("24234234234");
        $this->request->setText($title);
        $this->request->setUrl($url);
        $this->request->setLogo($logo);
        $this->request->setExt("{}");
        $resp = $this->top_client->execute($this->request);
        return $resp->data;
    }
    public function getSpreadClickUrl($url){
        $this->request = new TbkSpreadGetRequest;
        include_once ROOT_PATH . 'inc/ali/top/request/TbkSpreadRequest.php';
        $req = new TbkSpreadRequest;
        $req->url=$url;
        $this->request->setRequests(json_encode($req));
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }
    public function getNumiidByTbkPass($tbk_pass='【天猫品牌号】，复制这条信息￥sMCl0Yra3Ae￥后打开手机淘宝')
    {
        $this->request = new WirelessShareTpwdQueryRequest;
        $this->request->setPasswordContent($tbk_pass);
        $resp = $this->top_client->execute($this->request);
        return $resp;
    }
}
